package com.assignwork.service.impl;

import com.assignwork.dto.AssemblyLineDTO;
import com.assignwork.entity.AssemblyLine;
import com.assignwork.entity.DailyPlan;
import com.assignwork.mapper.AssemblyLineMapper;
import com.assignwork.service.*;
import com.assignwork.vo.AssemblyLineVO;
import com.assignwork.vo.StartedCabinVO;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * 作业线实现类
 */
@Service
@AllArgsConstructor
public class AssemblyLineServiceImpl extends BaseServiceImpl<AssemblyLineMapper, AssemblyLine> implements IAssemblyLineService {

    IJobConfigService jobConfigService;
    IStartedCabinService startedCabinService;
    ILineNoService lineNoService;
    IDailyPlanService dailyPlanService;

    @Override
    public List<AssemblyLineVO> listAssemblyLine(BladeUser bladeUser, Query query, AssemblyLineDTO search) {
        List<OrderItem> order = Condition.getOrder(query);
        order.add(OrderItem.asc("cabin_and_hatch"));
        for (OrderItem orderItem : order) {
            String column = orderItem.getColumn();
            if (column.equals("w_o_r_k_e_r")) {
                column = "work_node_type_key = 'WORKER' desc, work_number";
                orderItem.setColumn(column);
            } else if (column.equals("f_o_r_k_l_i_f_t")) {
                column = "work_node_type_key = 'FORKLIFT' desc, work_number";
                orderItem.setColumn(column);
            } else if (column.equals("c_r_a_n_e")) {
                column = "work_node_type_key = 'CRANE' desc , work_number";
                orderItem.setColumn(column);
            } else if (column.equals("t_r_a_c_t_o_r")) {
                column = "work_node_type_key = 'TRACTOR' desc , work_number";
                orderItem.setColumn(column);
            } else if (column.equals("l_o_a_d_e_r")) {
                column = "work_node_type_key = 'LOADER' desc , work_number";
                orderItem.setColumn(column);
            } else if (column.equals("e_x_c_a_v_a_t_o_r")) {
                column = "work_node_type_key = 'EXCAVATOR' desc , work_number";
                orderItem.setColumn(column);
            } else if (column.equals("i_n__c_a_b_i_n__m_a_c_h_i_n_e")) {
                column = "work_node_type_key = 'IN_CABIN_MACHINE' desc , work_number";
                orderItem.setColumn(column);
            }
        }
        List<AssemblyLineVO> assemblyLineVOS = this.baseMapper.listAssemblyLine(order, search);
        for (AssemblyLineVO assemblyLineVO : assemblyLineVOS) {
            List<StartedCabinVO> allStartedCabin = assemblyLineVO.getAllStartedCabin();
            for (StartedCabinVO startedCabinVO : allStartedCabin) {
                String hatch = startedCabinVO.getHatch();
                String hatchName = "";
                if (!hatch.equals("ALL")) {
                    hatchName = DictBizCache.getValue("hold_position", hatch);
                }
                startedCabinVO.setCabinName(DictBizCache.getValue("hold", startedCabinVO.getCabin()));
                startedCabinVO.setHatchName(hatchName);
                startedCabinVO.setCabinAndHatchName(startedCabinVO.getCabinName() + hatchName);
            }

            assemblyLineVO.setShiftKeyName(DictBizCache.getValue("time_space", assemblyLineVO.getShiftKey()));
            assemblyLineVO.setLoadTypeName(DictBizCache.getValue("load_type", assemblyLineVO.getLoadType()));
            assemblyLineVO.setPackageBulkName(DictBizCache.getValue("package_bulk", assemblyLineVO.getPackageBulkKey()));
        }
        return assemblyLineVOS;
    }

    @Override
    public boolean saveOrUpdateAssemblyLineInfo(BladeUser bladeUser, AssemblyLineDTO input) {
        if (input.getId() == null) {
            input.setLineNo(this.generateLineNo());
        }
        if (input.getPlanDate() == null) {
            DailyPlan byId = this.dailyPlanService.getById(input.getPlanId());
            if (byId != null) {
                input.setPlanDate(byId.getPlanDate());
            }
        }
        if (
                input.getPlanDate() != null &&
                        Func.isNotBlank(input.getShiftKey()) &&
                        AssemblyLineDTO.SHIFT_KEY_DAY.equals(input.getShiftKey())
        ) {
            input.setPlanDate(input.getPlanDate().plusDays(1));
        }
        this.saveOrUpdate(input);
        jobConfigService.saveOrUpdateDtoList(input.getJobConfig());
        startedCabinService.saveOrUpdateDtoList(input.getAllStartedCabin());
        return false;
    }

    @Override
    public boolean removeAssemblyLine(BladeUser bladeUser, AssemblyLineDTO input) {
        this.removeById(input.getId());
        jobConfigService.removeByIds(input.getJobConfigId());
        startedCabinService.removeByIds(input.getStartedCabinId());
        return false;
    }

    @Override
    public String generateLineNo() {
//        String today = DateUtil.today();
//        String s = this.baseMapper.maxLineNo(today);
//        if (Func.isBlank(s)) {
//            return today + "001";
//        }
//        return Func.toStr(Func.toLong(s) + 1l, "error");

        return lineNoService.getNewLineNo(LocalDate.now());
    }

}